<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class project extends ApplicationBase {
    private $pm     =   17;
    
    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_project');
        $this->load->model('m_perusahaan');
        $this->load->model('m_klien'); 
        $this->load->model('m_account');  
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        // load helpers
        
        // load js
        $this->smarty->load_javascript("resource/js/jquery/jquery-ui-1.9.2.custom.min.js");
        $this->smarty->load_style("jquery.ui/redmond/jquery-ui-1.8.13.custom.css");
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/project/list.html");
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/project/index/");
        $config['total_rows'] = $this->m_project->get_count_all_project();
        $config['uri_segment'] = 4;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array(($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_project->get_all_project_pagination($params));
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function add() {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/project/add.html");
        
        $rs_perusahaan = $this->m_perusahaan->get_all_perusahaan();
        $this->smarty->assign("rs_perusahaan", $rs_perusahaan);
        
        $rs_klien = $this->m_klien->get_all_klien();
        $this->smarty->assign("rs_klien", $rs_klien);
        
        $rs_pm = $this->m_account->get_all_user_by_role($this->pm);
        $this->smarty->assign("rs_pm", $rs_pm);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input  
        $this->tnotification->set_rules('project_name', 'Nama proyek', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('project_pm', 'PM proyek', 'trim|required'); 
        $this->tnotification->set_rules('perusahaan_id', 'Perusahaan', 'trim|required'); 
        $this->tnotification->set_rules('klien_id', 'Klien', 'trim|required');  
        // process 
        if ($this->tnotification->run() !== FALSE) { 
            // cek nama project    
            if ($this->m_project->is_exist_nama($this->input->post('project_name')) != NULL) {
                $this->tnotification->sent_notification("error", 'Nama project telah ada');
            }else{
                // parameter
                $params = array( 
                    $this->input->post('project_name'),
                    $this->input->post('project_pm'), 
                    $this->input->post('project_startdate'),
                    $this->input->post('project_enddate'),
                    $this->input->post('project_description'),
                    $this->input->post('project_kak'),
                    $this->input->post('project_cpklien'),
                    'start',
                    date('Y-m-d'),
                    $this->input->post('project_dateclose'),
                    $this->input->post('project_sosialisasi'), 
                    $this->input->post('project_pelatihan'), 
                    $this->input->post('perusahaan_id'),
                    $this->input->post('klien_id'),
                    $this->com_user['user_id']
                );
                // insert
                if ($this->m_project->insert($params)) {
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal disimpan");
                } 
            } 
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/project/add");
    }

    public function edit($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/project/edit.html");
        //data
        $result = $this->m_project->get_project_by_id($id);
        $this->smarty->assign("result", $result);
        
        $rs_perusahaan = $this->m_perusahaan->get_all_perusahaan();
        $this->smarty->assign("rs_perusahaan", $rs_perusahaan);
        
        $rs_klien = $this->m_klien->get_all_klien();
        $this->smarty->assign("rs_klien", $rs_klien);
        
        $rs_pm = $this->m_account->get_all_user_by_role($this->pm);
        $this->smarty->assign("rs_pm", $rs_pm);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        $this->tnotification->set_rules('project_name', 'Nama proyek', 'trim|required|max_length[100]');
        $this->tnotification->set_rules('project_pm', 'PM proyek', 'trim|required'); 
        $this->tnotification->set_rules('perusahaan_id', 'Perusahaan', 'trim|required'); 
        $this->tnotification->set_rules('klien_id', 'Klien', 'trim|required');  

        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array( 
                    $this->input->post('project_name'),
                    $this->input->post('project_pm'), 
                    $this->input->post('project_startdate'),
                    $this->input->post('project_enddate'),
                    $this->input->post('project_description'),
                    $this->input->post('project_kak'),
                    $this->input->post('project_cpklien'),  
                    $this->input->post('project_sosialisasi'), 
                    $this->input->post('project_pelatihan'), 
                    $this->input->post('perusahaan_id'),
                    $this->input->post('klien_id'),
                    $this->com_user['user_id'],
                    $this->input->post('project_id')
                );
            // insert
            if ($this->m_project->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/project/edit/" . $this->input->post('project_id'));
    }

    public function delete($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/project/delete.html");
        //data
        $result = $this->m_project->get_project_by_id($id);
        $this->smarty->assign("result", $result);
        
        $rs_perusahaan = $this->m_perusahaan->get_all_perusahaan();
        $this->smarty->assign("rs_perusahaan", $rs_perusahaan);
        
        $rs_klien = $this->m_klien->get_all_klien();
        $this->smarty->assign("rs_klien", $rs_klien);
        
        $rs_pm = $this->m_account->get_all_user_by_role($this->pm);
        $this->smarty->assign("rs_pm", $rs_pm);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('project_id', 'ID', 'trim|required|max_length[3]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('project_id')
            );
            // insert
            if ($this->m_project->delete($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/project/");
    }

} 